package cn.chencq.moudle.proxyip.callable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.chencq.moudle.proxyip.entity.ProxyIp;
import cn.chencq.moudle.proxyip.site.PageCrawlerService;
import cn.chencq.moudle.proxyip.site.ProxyListPageParser;

public class ProxyIpCallable implements Callable<List<ProxyIp>> {

	private static final Logger logger = LoggerFactory.getLogger(ProxyIpCallable.class);

	private String url;

	private int type;

	private ProxyListPageParser proxyListPageParser;

	private PageCrawlerService pageCrawlerService;

	public ProxyIpCallable() {
		// 默认构造器
	}

	public ProxyIpCallable(String url, int type, ProxyListPageParser proxyListPageParser,
			PageCrawlerService pageCrawlerService) {
		this.url = url;
		this.type = type;
		this.proxyListPageParser = proxyListPageParser;
		this.pageCrawlerService = pageCrawlerService;
	}

	@Override
	public List<ProxyIp> call() throws Exception {
		String html = null;
		List<ProxyIp> list = new ArrayList<ProxyIp>();
		if (1 == type) {
			html = pageCrawlerService.crawlerData(url, null);
		} else {
			html = pageCrawlerService.crawlerHtml(url, null);
		}

		list = proxyListPageParser.parse(html);
		logger.info("url:{}, 爬取到IP数量:{}", url, (list == null ? 0 : list.size()));
		
		return list;
	}

}
